package com.qipa.rabbitMq;

import com.qipa.config.RabbitConfig;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class MsgReceiver {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /*@RabbitListener(queues = "HelloWorldQueue")
    public void helloWorldReceive(Message message, Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        //模拟这里消费报错，然后手动确认
        try {
            String msgbody = new String(message.getBody());
            //1.接收转换消息
            System.out.println("直连模式消费者 1 收到消息  : " + msgbody + " 编号： " + deliveryTag);
            //2. 处理业务逻辑
            System.out.println("处理业务逻辑...");
//            int i = 10 / 0;
            //3. 手动签收
            channel.basicAck(deliveryTag, true);
        } catch (Exception e) {
            e.printStackTrace();
            //第三个参数：requeue：重回队列。如果设置为true，则消息重新回到queue，broker会重新发送该消息给消费端
            channel.basicNack(deliveryTag, true, true);
        }
    }*/


    @RabbitListener(queues = RabbitConfig.QUEUE_A1)
    public void processA(Message message, Channel channel) throws IOException  {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

//        int i = 10 / 0;
        logger.info("接收处理队列A当中的消息： " + message);
        //3. 手动签收
        channel.basicAck(deliveryTag, true);
    }


    @RabbitListener(queues = RabbitConfig.QUEUE_A2)
    public void processA2(String content) {
        logger.info("接收处理队列B当中的消息： " + content);
    }

}
